import Vue from 'vue'
import Router from 'vue-router'
import Home from '@/Home'

import {
  Message
} from 'element-ui';
import {
  getToken,
  getUserInfo
} from '@/request/token'
import store from '@/store'

Vue.use(Router)

const router = new Router({
  routes: [{
      path: '/write/:id?',
      component: r => require.ensure([], () => r(require('@/views/blog/BlogWrite')), 'blogwrite'),
      meta: {
        requireLogin: true
      },
    },
    {
      path: '',
      name: 'Home',
      component: Home,
      children: [{
          path: '/',
          component: r => require.ensure([], () => r(require('@/views/Index')), 'index')
        },
        {
          path: '/1',
          component: r => require.ensure([], () => r(require('@/views/Index')), 'index'),
          meta: {
            requireLogin: true
          },
        },
        {
            path: '/2',
            component: r => require.ensure([], () => r(require('@/views/Index')), 'index')
          },
        {
          path: '/3',
          component: r => require.ensure([], () => r(require('@/views/Index')), 'index'),
          meta: {
            requireLogin: true
          },
        },
        {
          path: '/log',
          component: r => require.ensure([], () => r(require('@/views/Log')), 'log')
        },
        {
          path: '/archives/:year?/:month?',
          component: r => require.ensure([], () => r(require('@/views/blog/BlogArchive')), 'archives')
        },
        {
          path: '/feedback',
          component: r => require.ensure([], () => r(require('@/views/MessageBoard')), 'messageboard')
        },
        {
          path: '/view/:id',
          component: r => require.ensure([], () => r(require('@/views/blog/BlogView')), 'blogview')
        },
        {
          path: '/:type/all',
          component: r => require.ensure([], () => r(require('@/views/blog/BlogAllCategoryTag')), 'blogallcategorytag')
        },
        {
          path: '/:type/:id',
          component: r => require.ensure([], () => r(require('@/views/blog/BlogCategoryTag')), 'blogcategorytag')
        }
      ]
    },
    {
      path: '/login',
      component: r => require.ensure([], () => r(require('@/views/Login')), 'login')
    },
    {
      path: '/register',
      component: r => require.ensure([], () => r(require('@/views/Register')), 'register')
    }
  ],
  scrollBehavior(to, from, savedPosition) {
    return {
      x: 0,
      y: 0
    }
  }
})

router.beforeEach((to, from, next) => {
  if (getToken() && getUserInfo()) { // 有token
    if (to.path === '/login') {
      next({
        path: '/'
      })
    } else {
      next()
    }
  } else { // 无token
    if (to.matched.some(r => r.meta.requireLogin)) {
      Message({
        type: 'warning',
        showClose: true,
        message: '请先登录哦'
      })
    } else {
      next();
    }
  }
})

export default router
